#include <QPixmap>
#include <QFileDialog>
#include "_SetMainpageCallback.h"
#include "AnGlobal.h"
#include "AnAdbServer.h"

_SetMainpageCallback::_SetMainpageCallback(AnMainwin * anMainwin)
{
    amw = anMainwin;
}

void _SetMainpageCallback::response(QJsonObject an_resp)
{

    //检查设备信息是否获取成功，成功则将信息显示到界面上
    if (an_resp.value(DATA).toObject().value(RESULT).toInt()) {
        amw->mp->label_model->setText(an_resp.value(DATA).toObject()
                                      .value("model").toString());
        amw->mp->label_manuf->setText(an_resp.value(DATA).toObject()
                                      .value("manufacturer").toString());
        amw->mp->label_kernel->setText(an_resp.value(DATA).toObject()
                                      .value("kernel").toString());
        amw->mp->label_sysversion->setText(an_resp.value(DATA).toObject()
                                      .value("android_version").toString());
    }
}


void _SetMainpageCallback::connect_close()
{
    //连接关闭时清空界面信息
    amw->mp->label_model->setText("未连接");
    amw->mp->label_manuf->setText("未连接");
    amw->mp->label_kernel->setText("未连接");
    amw->mp->label_sysversion->setText("未连接");
}


void _SetMainpageCallback::connect_build()
{
    //连接建立时发送获取设备信息的请求
    QJsonObject reqs;
    reqs.insert(NAME, QJsonValue("devices_info"));
    reqs.insert(STATUS, QJsonValue(REQUEST));

    adb_server->adbServerManager(reqs, this);
}



_ScreencapCallback::_ScreencapCallback(AnMainwin * anMainwin)
{
    amw = anMainwin;
    device_not_connect = QPixmap(":/img/device_not_connect.png");
}

void _ScreencapCallback::response(QJsonObject an_resp)
{
    if (an_resp.value(DATA).toObject()
            .value(RESULT).toInt()) {
        QPixmap pixmap(SCREENCAP_LOC_PC);
        if (pixmap.isNull()) {
            return;
        }

        if (((double)pixmap.width()) / pixmap.height() > ((double)SCREENCAP_W)/SCREENCAP_H) {
            pixmap = pixmap.scaledToWidth(SCREENCAP_W, Qt::SmoothTransformation);
        } else {
            pixmap = pixmap.scaledToHeight(SCREENCAP_H, Qt::SmoothTransformation);
        }

        amw->mp->label_screenshot->setPixmap(pixmap);
    }

}


void _ScreencapCallback::save_clicked()
{
    if (AnGlobal::is_connecting() && QFile::exists(SCREENCAP_LOC_PC)) {
        QString dest_file_path = QFileDialog::getSaveFileName(amw, "保存截图", ".", "Images (*.png)");
        if (!dest_file_path.isNull() && QFile::exists(dest_file_path)) {
            if (!QFile::remove(dest_file_path)) {
                return;
            }
        }
        QFile::copy(SCREENCAP_LOC_PC, dest_file_path);
    }


}


void _ScreencapCallback::refresh_clicked()
{
    if (AnGlobal::is_connecting() && !AnGlobal::is_request_processing("screencap")) {
        QJsonObject reqs;
        reqs.insert(NAME, QJsonValue("screencap"));
        reqs.insert(STATUS, QJsonValue(REQUEST));
        adb_server->adbServerManager(reqs, this);
    }

}


void _ScreencapCallback::connect_close()
{
    amw->mp->label_screenshot->setPixmap(device_not_connect);
}


